package com.only4play.portal.cp.api.impl;

import com.only4play.common.utils.HexUtils;
import com.only4play.portal.common.error.PortalErrorException;
import com.only4play.portal.common.session.PortalSession;
import com.only4play.portal.common.session.PortalSessionManager;
import com.only4play.portal.common.session.StandardSessionManager;
import com.only4play.portal.common.util.http.RequestHttp;
import com.only4play.portal.cp.api.CpService;
import com.only4play.portal.cp.config.CpConfigStorage;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;

/**
 * @author liyuncong
 * @version 1.0
 * @file BaseCpServiceImpl
 * @brief BaseCpServiceImpl
 * @details BaseCpServiceImpl
 * @date 2024-06-11
 *
 * Edit History
 * ----------------------------------------------------------------------------
 * DATE                     NAME               DESCRIPTION
 * 2024-06-11               liyuncong          Created
 */

@Slf4j
@Setter
public abstract class BaseCpServiceImpl<H, P> implements CpService, RequestHttp<H, P> {

    /**
     * The global access token lock being refreshed
     */
    protected final Object globalAccessTokenRefreshLock = new Object();

    protected CpConfigStorage configStorage;

    private PortalSessionManager sessionManager = new StandardSessionManager();

    @Override
    public boolean checkSignature(String msgSignature, String timestamp, String nonce, String data) {
        try {
            // 生成签名
            String[] tmp = new String[4];
            tmp[0] = this.configStorage.getToken();
            tmp[1] = timestamp;
            tmp[2] = nonce;
            tmp[3] = data;

            Arrays.sort(tmp);
            StringBuilder sb = new StringBuilder(4);
            for (String t : tmp) {
                sb.append(t);
            }
            String encoded = HexUtils.encodeHex(sb.toString().getBytes(StandardCharsets.UTF_8));
            return encoded.equals(msgSignature);
        } catch (Exception exception) {
            log.error("Checking signature failed", exception);
            return false;
        }
    }

    @Override
    public PortalSession getSession(String id) {
        if (this.sessionManager == null) {
            return null;
        }
        return this.sessionManager.getSession(id);
    }

    @Override
    public PortalSession getSession(String id, boolean create) {
        if (this.sessionManager == null) {
            return null;
        }
        return this.sessionManager.getSession(id, create);
    }

    @Override
    public PortalSessionManager getSessionManager() {
        return this.sessionManager;
    }

    @Override
    public void setSessionManager(PortalSessionManager sessionManager) {
        this.sessionManager = sessionManager;
    }

    @Override
    public String getAccessToken() throws PortalErrorException {
        return getAccessToken(false);
    }

    @Override
    public void setCpConfigStorage(CpConfigStorage configStorage) {
        this.configStorage = configStorage;
        this.initHttp();
    }

    @Override
    public String get(String url, String queryParam) throws PortalErrorException {
        return null;
    }

    @Override
    public String post(String url, String postData) throws PortalErrorException {
        return null;
    }
}
